package base;

import java.util.Random;

/**
 * Generate Gauss number via the nextInt() method.
 * 
 * @author rweiller
 */
public class Gauss
{
	public Random	x;
	public Random	y;
	public int		m;
	public int		e;

	/**
	 * Constructor : Creates a Gauss generator
	 * 
	 * @param seed_x the first seed
	 * @param seed_y the second seed
	 * @param m the medium
	 * @param e the deviation
	 * @throws IllegalArgumentException if m minor e
	 */
	public Gauss(int seed_x, int seed_y, int m, int e) throws IllegalArgumentException
	{
		if (m < 1)
		{
			throw new IllegalArgumentException("Config file : gauss.rd.seed.m must be > 0");
		} else
			if (m - e < 0)
			{
				throw new IllegalArgumentException("Config file : m should be strictly higher than e");
			}

		this.x = new Random(seed_x);
		this.y = new Random(seed_y);
		this.m = m;
		this.e = e;
	}

	/**
	 * Generates a number using Gauss probability law
	 * 
	 * @return a Gauss number
	 */
	public double nextDouble()
	{
		double gaussNumber;
		do
		{
			gaussNumber = m + e * (Math.cos(2 * Math.PI * x.nextDouble()) * Math.sqrt(-2 * Math.log(y.nextDouble())));
		} while (gaussNumber < 0);
		return gaussNumber;
	}
}
